<template>
  <div class="app-container">
    <el-card class="mb20">
      <el-form inline>
        <el-form-item label="工艺名称">
          <el-input v-model="keyword" placeholder="工艺名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button type="success" @click="handleAdd">新增</el-button>
      <el-table :data="list">
        <el-table-column
          v-for="item in listC"
          :key="item.prop"
          :label="item.label"
          :prop="item.prop"
        ></el-table-column>
        <el-table-column label="操作" v-slot="{ row }">
          <el-button type="primary" @click="handleEdit(row)" size="mini"
            >修改</el-button
          >
          <el-button type="danger" @click="handleDel(row.id)" size="mini"
            >删除</el-button
          >
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: right; margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pager.page"
        :page-sizes="[2, 10, 20, 30]"
        :page-size="pager.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.total"
      >
      </el-pagination>
    </el-card>
    <el-dialog :title="title" center :visible.sync="visible" @close="close">
      <el-form inline ref="listERef" :model="listE">
        <el-form-item label="产品" prop="product" required>
          <el-input v-model="listE.product" placeholder="产品"></el-input>
        </el-form-item>
        <el-form-item label="工艺名称" prop="name" required>
          <el-input v-model="listE.name" placeholder="工艺名称"></el-input>
        </el-form-item>
        <el-form-item label="单位耗电量" prop="powerConsumption" required>
          <el-input
            v-model="listE.powerConsumption"
            placeholder="单位耗电量"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            :rows="3"
            resize="none"
            v-model="listE.remark"
            placeholder="备注"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            type="textarea"
            :rows="3"
            resize="none"
            v-model="listE.description"
            placeholder="描述"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button @click="close">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getProcessModelingList, editProcessModelingList, addProcessModelingList, delProcessModelingList } from '@/api_zc/mes/process-modeling'
export default {
  mounted () {
    this.getList()
  },
  data () {
    return {
      list: [],
      keyword: '',
      listC: [
        {
          label: '编号',
          prop: 'id'
        },
        {
          label: '工艺名称',
          prop: 'name'
        },
        {
          label: '产品',
          prop: 'product'
        },
        {
          label: '型号',
          prop: 'model'
        },
        {
          label: '规格',
          prop: 'specification'
        },
        {
          label: '单位',
          prop: 'unit'
        },
        {
          label: '单位耗电量',
          prop: 'powerConsumption'
        },
        {
          label: '备注',
          prop: 'remark'
        },
      ],
      listE: {
        product: '',
        name: '',
        powerConsumption: '',
        remark: '',
        description: ''
      },
      pager: {
        limit: 10,
        page: 1,
        total: 0
      },
      visible: false,
      title: '',
      isAdd: false
    }
  },
  methods: {
    async getList () {
      const { data: { content, total } } = await getProcessModelingList({ keyword: this.keyword, page: this.pager.page, limit: this.pager.limit })
      this.list = content
      this.pager.total = total
    },
    handleReset () {
      this.keyword = ''
      this.getList()
    },
    handleSizeChange (val) {
      this.pager.limit = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.pager.page = val
      this.getList()
    },
    close () {
      this.visible = false
    },
    handleEdit (row) {
      this.visible = true
      this.title = "修改工艺建模"
      this.isAdd = false
      this.$nextTick(() => {
        Object.assign(this.listE, row)
      })
    },
    handleAdd (row) {
      this.visible = true
      this.title = "修改工艺建模"
      this.isAdd = true
      this.$nextTick(() => {
        this.$refs.listERef.resetFields()
      })
    },
    async handleDel (id) {
      await delProcessModelingList({ ids: [id] })
      this.$message.success('删除成功')
      this.getList()
    },
    submit () {
      this.$refs.listERef.validate(async (callback) => {
        if (!callback) {
          return this.$message.error('请填写必要字段')
        }
        if (this.isAdd) {
          await addProcessModelingList(this.listE)
          this.$message.success('添加成功')
        } else {
          await editProcessModelingList(this.listE)
          this.$message.success('修改成功')
        }
        this.visible = false
        this.getList()
      })

    }
  }
}
</script>

<style lang="scss">
</style>